/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.model.DistributionUserIncome;
import com.yami.shop.distribution.common.param.RangeTimeParam;
import com.yami.shop.distribution.common.service.DistributionUserIncomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionUserIncome")
@Api(tags = "平台端分销员收入接口")
public class DistributionUserIncomeController {

    @Autowired
    private DistributionUserIncomeService distributionUserIncomeService;

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserIncome:page')")
    @ApiOperation(value = "分页获取分销员收入记录列表")
    public ServerResponseEntity<IPage<DistributionUserIncome>> page(DistributionUserIncome distributionUserIncome,
                                                              PageParam<DistributionUserIncome> page) {
        IPage<DistributionUserIncome> list = distributionUserIncomeService.page(page, new LambdaQueryWrapper<DistributionUserIncome>());
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/page/anduser")
    @ApiOperation(value = "分页获取分销员以及收入记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userMobile", value = "分销员手机号码", dataType = "String"),
            @ApiImplicitParam(name = "orderNumber", value = "订单编号", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "收入状态", dataType = "Integer")
    })
    public ServerResponseEntity<IPage<DistributionUserIncome>> page(PageParam<DistributionUserIncome> page,
                                                              RangeTimeParam rangeTimeParam, String userMobile, String orderNumber,
                                                              Integer state, DistributionUserIncome distributionUserIncome) {
        IPage<DistributionUserIncome> list = distributionUserIncomeService.incomeAndDistributionUserPage(page, Constant.PLATFORM_SHOP_ID,
                rangeTimeParam, userMobile, orderNumber, state, distributionUserIncome);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserIncome:info')")
    @ApiOperation(value = "根据id获取分销员收入信息")
    @ApiImplicitParam(name = "id", value = "分销员收入id", dataType = "Long")
    public ServerResponseEntity<DistributionUserIncome> info(@PathVariable("id") Long id) {
        DistributionUserIncome distributionUserIncome = distributionUserIncomeService.getById(id);
        return ServerResponseEntity.success(distributionUserIncome);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserIncome:save')")
    @ApiOperation(value = "保存分销员收入信息")
    public ServerResponseEntity<Void> save(@RequestBody @Valid DistributionUserIncome distributionUserIncome) {
        distributionUserIncomeService.save(distributionUserIncome);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserIncome:update')")
    @ApiOperation(value = "修改分销员收入信息")
    public ServerResponseEntity<Void> update(@RequestBody @Valid DistributionUserIncome distributionUserIncome) {
        distributionUserIncomeService.updateById(distributionUserIncome);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserIncome:delete')")
    @ApiOperation(value = "根据id删除分销员收入信息")
    @ApiImplicitParam(name = "id", value = "分销员收入id", dataType = "Long")
    public ServerResponseEntity<Void> delete(@PathVariable Long id) {
        distributionUserIncomeService.removeById(id);
        return ServerResponseEntity.success();
    }
}
